package com.schoolcloud.social.controller.event;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.social.dto.SocEventCreateDTO;
import com.schoolcloud.social.entity.SocEvents;
import com.schoolcloud.social.query.EventQuery;
import com.schoolcloud.social.service.EventInteractionService;
import com.schoolcloud.social.service.SocEventsService;
import com.schoolcloud.social.vo.EventDetailVO;
import com.schoolcloud.social.vo.EventPersonInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/social/event")
@RequiredArgsConstructor
public class SocEventController {
    private final SocEventsService socEventService;
    private final EventInteractionService eventInteractionService;

    @PostMapping
    public R addEvent(@RequestBody SocEventCreateDTO socEvent) {
        eventInteractionService.addEvent(socEvent);
        return R.ok().setMsg("发布成功！");
    }

    @GetMapping("/{id}")
    public R<SocEvents> getEventById(@PathVariable Integer id) {
        SocEvents socEvent = socEventService.getById(id);
        return R.ok(socEvent);
    }

    @GetMapping("/detail/{eventId}")
    public R<EventDetailVO> getEventDetailById(@PathVariable Integer eventId) {
        EventDetailVO socEvent = eventInteractionService.getEventDetailById(eventId);
        return R.ok(socEvent);
    }

    @PutMapping
    public R updateEvent(@RequestBody SocEventCreateDTO socEvent) {
        eventInteractionService.updateEvent(socEvent);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R deleteEvent(@PathVariable Integer id) {
        socEventService.removeById(id);
        return R.ok();
    }

    @GetMapping("/page")
    public R<PageDTO<EventDetailVO>> getEventPage(EventQuery query) {
        PageDTO<EventDetailVO> eventPage = eventInteractionService.getEventVOPage(query);
        return R.ok(eventPage);
    }

    @GetMapping("/person/{userId}")
    public R<EventPersonInfoVO> getEventPersonInfo(@PathVariable(value = "userId", required = false) Integer userId) {
        if (userId == null) {
            userId = UserContext.getUserToInteger();
        }
        EventPersonInfoVO eventPersonInfo = eventInteractionService.getEventPersonInfo(userId);
        return R.ok(eventPersonInfo);
    }
}
